package models

import (
	"log"

	"xorm.io/builder"
)

type OaUser struct {
	Id          int    `xorm:"not null pk autoincr comment('自增主键') INT(11)" json:"id"`
	Name        string `json:"name"`
	Type        string `json:"type"`
	Province    string `json:"province"`
	City        string `json:"city"`
	District    string `json:"district"`
	Password    string `json:"password"`
	Registernum int    `json:"registernum"`
	Usednum     int    `json:"usednum"`
	SchoolList  string `json:"school_list"`
}

func (o *OaUser) GetUserByCondition(condition builder.Cond) (users OaUser, user bool, err error) {
	user, err = engine.Table("oa_user").And(condition).Get(&users)
	log.Println(user, err)
	return
}

func (o *OaUser) GetByUser(user_id string) (users OaUser, err error) {
	_, err = engine.Table("oa_user").And("id = ?", user_id).Get(&users)
	return
}

func (o *OaUser) UpdateNumber(id int, number int) bool {
	_, err := engine.Exec("update oa_user set usednum = ? where id = ?", number, id)
	log.Println(err)
	if err != nil {
		return false
	}
	return true
}

func (o *OaUser) GetUserExist(filter builder.Cond) (has bool, list OaUser, err error) {
	has, err = engine.Table("oa_user").
		//Where("isused = ?", "1").
		And(filter).Get(&list)
	log.Println(has, err)
	return has, list, err
}

func (o *OaUser) Add() bool {
	if _, err := engine.Insert(o); err != nil {
		return false
	}
	return true
}

func (o *OaUser) Edit(who ...string) bool {
	if _, err := engine.Id(o.Id).Cols(who...).Update(o); err != nil {
		return false
	}
	return true
}
